#include "theater.h"
/************************************************
 *
 *  start for concessionClerk
 *
 ************************************************/
 int  main()

{
    int i;
    int ccId;
    Employee* currentCC;
    int temp;
    int temp1;
    /* get curTickclerkno */
    initTheater();
    AcquireLock(concessionClerkWorknoLock);
    temp=getMV(concessionClerkWorkNO);
    ccId = temp++;
    currentCC=&cc[ccId];
    currentCC->id = ccId;
    setMV(concessionClerkWorkNO,temp);
    Printf("concession clerk %d is going to work\n",ccId);
    ReleaseLock(concessionClerkWorknoLock);
    while(1)
    {
        AcquireLock(lBuyFood);
        if (getIsBreak(currentCC))
        {
            /*if clerk is on break,wait for manager signal*/
            Printf("concessionclerk [%d] is going on break.\n", currentCC->id);
            temp=getMV(breakConcessionClerk);
            if (!temp) {
                WaitCV(lBuyFood, cBreakConcessionClerk);
            }
           setMV(breakConcessionClerk , 0);
            Printf("concessionclerk [%d] is coming off break.\n", currentCC->id);
            setMV(currentCC->isBreak,false);
        }
        /* if have customer, then set busy and signal the customer, else set freewewesws*/
        temp=getMV(currentCC->waitingSize);
        if ( temp> 0)
        {
            setMV(currentCC->isBusy,true);
            Printf("ConcessionClerk [%d] has a line length [%d] and is signaling a customer.\n",currentCC->id,temp);
            /*subWaitingSize();*/
            SignalCV(lBuyFood,currentCC->lineCondition);
        }
        else
        {
            Printf("ConcessionClerk [%d] has no one in line. I am available for a customer.\n",currentCC->id);
            setMV(currentCC->isBusy,false);
        }
        AcquireLock(currentCC->lock);
        /* on service*/
        ReleaseLock(lBuyFood);
        /* wait customer or manager to signal*/
        WaitCV(currentCC->lock,currentCC->msgCondition);
	 if(true==getMV(closeTheater)){
		ReleaseLock(currentCC->lock);
		break;
	}
        /* if break         */
        AcquireLock(lBuyFood);
        if (getIsBreak(currentCC) && !getIsBusy(currentCC))
        {
            setMV(currentCC->isBusy,false);
            ReleaseLock(currentCC->lock);
            ReleaseLock(lBuyFood);
            continue;
        }
        else
        {
           setMV(currentCC->isBusy,true);
        }
        ReleaseLock(lBuyFood);
        SignalCV(currentCC->lock,currentCC->msgCondition);
        WaitCV(currentCC->lock,currentCC->msgCondition);
        /* tell customers food price */
        temp=getMV(currentCC->food[0]);
        temp1=getMV(currentCC->food[1]);
        setMV( currentCC->amount , temp*PRICE_POPCORN +temp1*PRICE_SODA);
        Printf("ConcessionClerk [%d] has an order for [%d] popcorn and [%d] soda. The cost is [%d].\n",currentCC->id,temp,temp1,temp*PRICE_POPCORN +temp1*PRICE_SODA);

        SignalCV(currentCC->lock,currentCC->msgCondition);
        /* wait for customer pay*/
        WaitCV(currentCC->lock,currentCC->msgCondition);
        /* amount*/
        AcquireLock(lAmount);
        temp=getMV(currentCC->amount);
        addMVbyValue(concessionClerkAmount[currentCC->id],temp);
        ReleaseLock(lAmount);
        /* get money, handout the food*/
        SignalCV(currentCC->lock,currentCC->msgCondition);
        Printf("ConcessionClerk [%d] has been paid for the order.\n",currentCC->id);
        /* customer leave, get next customer*/
        WaitCV(currentCC->lock,currentCC->msgCondition);
        ReleaseLock(currentCC->lock);
    }
    Exit(0);
}

